package put.poznan.qmese.ticket.control;

import put.poznan.qmese.ticket.machine.TicketMachine;
import put.poznan.qmese.ticket.model.Ticket;
import put.poznan.qmese.ticket.model.TicketFactory;

public class Control {

	private TicketMachine machine;

	// TODO: redundant but how to get needed cash??!!
	private TicketFactory factory;

	public void handle(String command) {

		machine = new TicketMachine(0);
		factory = new TicketFactory();

		try {
			int option = Integer.valueOf(command);

			if (option == 7) {
				System.out.println(machine.getCurrentCash());
			} else if (option >= 1 && option < 7) {
				Ticket t = factory.build(option);
				System.out.println("Give me " + t.getCost() + "PLN!");

				Input input = new ConsoleInput();
				Control ctrl = new Control();

				String i = input.readCommand();
				System.out.println("Thanks!");
				int inputCash = Integer.valueOf(i);

				t = machine.buyTicket(Integer.valueOf(command), inputCash);
				if (t == null) {
					System.out.println("Give me more money!");
				} else {
					System.out.println(t.toString());
				}
			} else {
				System.out.println("Wrong option!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
